//
//  QWViewController.m
//  Datang
//
//  Created by ZhengYing on 16/9/27.
//  Copyright © 2016年 test. All rights reserved.
//

#import "QWViewController.h"

@interface QWViewController ()
{
    int seleIndex;
    int m_page;
    NSTimer *m_timer;
    BOOL animating;
    int indexCount;
}

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation QWViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    for (UIButton *btn in self.view.subviews) {
        if(btn.tag >100){
            [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    
    NSString *imagePath=[NSString stringWithFormat:@"skin/qw/1/1.jpg"];
    imagePath=[self getResourcePath:imagePath];
    
    self.imageView.image=[UIImage imageWithContentsOfFile:imagePath];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)click:(UIButton *)sender{
    m_page=0;
    indexCount = 0;

    switch (sender.tag-100) {
        case 1:
            {
                indexCount = 209;

            }
            break;
        case 2:
        {
            indexCount = 180;

        }
            break;
        case 3:
        {
            indexCount = 180;

        }
            break;
        case 4:
        {
            indexCount = 214;

        }
            break;
        case 5:
        {
            indexCount = 214;

        }
            break;
        case 6:
        {
            indexCount = 214;
 
        }
            break;
        case 7:
        {
            indexCount = 214;

        }
            break;
        case 8:
        {
            indexCount = 214;

        }
            break;
        case 9:
        {
            indexCount = 219;

        }
            break;
       
            
        default:
            break;
    }
    [self playPressBySender:sender];
}
-(void)playPressBySender:(id)sender{
    UIButton *btn = (UIButton *)sender;
    seleIndex = btn.tag-100;
    [self starAnimations];
}

-(void)starAnimations
{
    
    
    
    if (animating == NO) {
        if (!m_timer) {
            m_timer=[NSTimer scheduledTimerWithTimeInterval:1/24.0
                                                     target:self
                                                   selector:@selector(timerFinImage)
                                                   userInfo:nil repeats:YES];
            animating=YES;
            
        }
    }
    //    else{
    //
    //        [m_timer invalidate];
    //        m_timer=nil;
    //        animating=NO;
    //    }
    
}


-(void)timerFinImage{
    
    NSString *imagePath=[NSString stringWithFormat:@"skin/qw/%d/%d.jpg",seleIndex,m_page];
    imagePath=[self getResourcePath:imagePath];
    
    self.imageView.image=[UIImage imageWithContentsOfFile:imagePath];
    
    if (m_page == indexCount) {
        [m_timer invalidate];
        m_timer=nil ;
        animating=NO;
        m_page=0;
        
    }
    else{
        m_page+=1;
        
    }
    
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
